//
//  SFStatusViewModel.swift
//  SFWeiboPro
//
//  Created by happy on 2017/9/21.
//  Copyright © 2017年 happy. All rights reserved.
//

import Foundation


class SFStatusViewModel {
    
    var status: SFStatus
    
    var memberImage: UIImage?
    
    /// vip图片
    var vipImage: UIImage?
    
    /// 转发数
    var retweetStr: String?
    /// 评论数
    var commentStr: String?
    
    /// 点赞数
    var likeStr: String?
    
    
    /// 图片视图尺寸
    var pictureSize: CGSize?
    
    
    init(model: SFStatus) {
        
        self.status = model
        
        let memberRank = model.user?.mbrank ?? 0
        
        if memberRank > 0 && memberRank < 7 {
            let imageName = "common_icon_membership_level\(memberRank)"
            memberImage = UIImage(named: imageName)
        }
        
        switch model.user?.verified_type ?? -1 {
        case 0:
            vipImage = UIImage(named: "avatar_vip")
        case 2, 3, 5:
            vipImage = UIImage(named: "avatar_enterprise_vip")
        case 220:
            vipImage = UIImage(named: "avatar_grassroot")
        default:
            break
        }
        
//        model.reposts_count = Int(arc4random_uniform(100000)) //测试过万的数据
        retweetStr = countToString(model.reposts_count, "转发")
        commentStr = countToString(model.comments_count, "评论")
        likeStr = countToString(model.attitudes_count, "赞")
        
        pictureSize = calcPictureViewSize(count: model.pic_urls?.count)
    }
    
    
    /// 计算图片视图的size
    ///
    /// - Parameter count: 图片的个数
    /// - Returns: 返回视图的size
    private func calcPictureViewSize(count: Int?) -> CGSize {
        
        if count == 0 || count == nil {
            return CGSize()
        }
        
        let row = (count! - 1)/3 + 1 //行数
        
        /// 计算高度
        let SFStautsPictureViewHeight = SFStatusPictureViewOutterMargin
            + CGFloat(row) * SFStatusPictureItemWidth
            + CGFloat(row - 1) * SFStatusPictureViewInnerMargin
        
        return CGSize(width: SFStatusPictureViewWidth, height: SFStautsPictureViewHeight)
        
    }
    
    /// 数字转字符串
    ///
    /// - Parameters:
    ///   - count: 点赞和评论数
    ///   - defaultStr: 默认字符串显示
    /// - Returns: 转换后的字符串
    private func countToString(_ count: Int, _ defaultStr: String) -> String {
        
        if count == 0 {
            return defaultStr
        }
        
        if count < 10000 {
            return count.description
        }
        
        return String(format: "%0.2f 万", Double(count) / 10000)
    }
    
}
